package com.zhudi.controller;

import com.zhudi.commen.BaseResponse;
import com.zhudi.commen.ErrorCode;
import com.zhudi.commen.ResultUtils;
import com.zhudi.domain.User;
import com.zhudi.domain.request.UserLoginRequest;
import com.zhudi.domain.request.UserRegisterRequest;
import com.zhudi.expection.BusinessException;
import com.zhudi.mapper.UserMapper;
import com.zhudi.service.UserService;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.zhudi.contant.UserContant.USER_LOGIN_STATE;

/**
 * 用户用户前端的控制器
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {


    @Resource
    UserService userService;


    /**
     * 注册接口
     * @param userLoginRequest 封装的登录数据传输类
     * @param request 浏览器request
     * @return 登录成功的话返回用户的相关信息
     */
    @PostMapping("/login")
    public BaseResponse<User> userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request){
        log.info("login传入的用户名为"+userLoginRequest.getUser_name());
        log.info("login传入的密码为"+userLoginRequest.getPassword());

        if (StringUtils.isAnyBlank(userLoginRequest.getUser_name(),userLoginRequest.getPassword())){
            throw new BusinessException(ErrorCode.USER_ERROR,"账户或密码为空");
        }

       return userService.login(userLoginRequest.getUser_name(),userLoginRequest.getPassword(),request);
    }

    /**
     * 注册功能
     * @param request 浏览器request
     * @return 注册成功返回用户id，失败返回空
     */
    @PostMapping("/register")
    public BaseResponse<Integer> userRegister(@RequestBody UserRegisterRequest userRegisterRequest, HttpServletRequest request){

        if (StringUtils.isAnyBlank(userRegisterRequest.getUser_name(),userRegisterRequest.getPassword(),userRegisterRequest.getConfirmPassword())){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"注册信息参数不足");
        }

        return userService.register(userRegisterRequest.getUser_name(),userRegisterRequest.getPassword(),userRegisterRequest.getConfirmPassword(),request);
    }

    /**
     * 注销功能
     * @param request 浏览器request
     * @return 成功返回0
     */
    @PostMapping("/logout")
    public BaseResponse<Integer> userLogout(HttpServletRequest request){
        return ResultUtils.success(userService.logout( request ),"注销成功");
    }

    @GetMapping("/showAll")
    public BaseResponse<List<User>> getAllUser(){
        return ResultUtils.success(userService.getAllUser(),"获取脱敏后的所有用户信息成功");
    }

    @GetMapping("/currentUser")
    public BaseResponse<User> getCurrentUser(HttpServletRequest httpServletRequest){
        Object userobj = httpServletRequest.getSession().getAttribute(USER_LOGIN_STATE);
        User user = (User) userobj;
        if (user==null){
            //todo:这里以后要改为抛出一个异常，统一捕获
            throw new BusinessException(ErrorCode.NOT_LOGIN);
        }
        log.info("调用了getCurrentUser");
        return ResultUtils.success(userService.getCurrentUser(user.getId()),"获取脱敏后的用户信息成功");

    }

    @PostMapping("/update")
    public BaseResponse<Integer> update(User updateUser,User loginUser){
        return null;
    }
}
